package com.sgb.kjwl.viewmodel.self.packers;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.self.SelfTeamIntroduceEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfMainBusinessActivity;
import com.sgb.kjwl.view.widget.TeamMultiPopWindow;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 * @author WYJ
 * @date 2020/8/27
 */
public class SelfMainBusinessModel extends BaseViewModel implements TeamMultiPopWindow.FilterResultClick, TeamMultiPopWindow.IItemClick {

    private SelfRepertory mRepertory;
    private SelfMainBusinessActivity mActivity;

    private TeamMultiPopWindow mPopWindow;

    public SelfMainBusinessModel(SelfMainBusinessActivity mActivity, SelfRepertory mRepertory) {
        this.mRepertory = mRepertory;
        this.mActivity = mActivity;
    }

    public void getCategory(String cid, boolean isAll) {
        Call<BaseEntity<List<PickSelectEntity>>> call;
        if (mPopWindow == null) {
            DialogHelper.showProgressDialog(mActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
            call = APIManager.getInstance().getPackerAPI().getProTypeList(mRepertory.getAreaCode());
        } else {
            call = APIManager.getInstance().getPackerAPI().getSecondCategory(cid);
        }
        call.enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    updateData(response.body().getData(), isAll);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    private void updateData(List<PickSelectEntity> data, boolean isAll) {
        if (data == null || data.size() == 0) {
            return;
        }
        if (mPopWindow == null) {
            //初始化筛选
            mPopWindow = new TeamMultiPopWindow(mActivity);
            mPopWindow.setFilterResultClick(this);
            mPopWindow.onItemClick(this);
        }
        switch (mPopWindow.getType()) {
            case TeamMultiPopWindow.TYPE_DEFAULT:
                mPopWindow.setData1(data);
                break;
            case TeamMultiPopWindow.TYPE_TWO:
                mPopWindow.setData2(data);
                break;
            case TeamMultiPopWindow.TYPE_THR:
                mPopWindow.setData3(data);
                break;
            case TeamMultiPopWindow.TYPE_FOU:
                mPopWindow.setData4(data);
                break;
            case TeamMultiPopWindow.TYPE_FIV:
                mPopWindow.setData5(data);
                break;
            case TeamMultiPopWindow.TYPE_SIX:
                mPopWindow.setData6(data);
                break;
            default:
                break;
        }
    }

    @Override
    public void result(PickSelectEntity... entity) {
        if (entity == null || entity.length == 0) {
            return;
        }
        int length = entity.length;
        SelfTeamIntroduceEntity.BusinessScopesBean bean = new SelfTeamIntroduceEntity.BusinessScopesBean();
        for (int i = 0; i < length; i++) {
            switch (i) {
                case 0:
                    bean.setFirstCategoryName(entity[i].getProjectscale());
                    bean.setFirstCategoryNo(entity[i].getNo());
                    break;
                case 1:
                    bean.setSecondCategoryName(entity[i].getProjectscale());
                    bean.setSecondCategoryNo(entity[i].getNo());
                    break;
                case 2:
                    bean.setThirdCategoryName(entity[i].getProjectscale());
                    bean.setThirdCategoryNo(entity[i].getNo());
                    break;
                case 3:
                    bean.setFourCategoryName(entity[i].getProjectscale());
                    bean.setFourCategoryNo(entity[i].getNo());
                    break;
                case 4:
                    bean.setFifthCategoryName(entity[i].getProjectscale());
                    bean.setFifthCategoryNo(entity[i].getNo());
                    break;
                case 5:
                    bean.setSixthCategoryName(entity[i].getProjectscale());
                    bean.setSixthCategoryNo(entity[i].getNo());
                    break;
            }
        }
        mActivity.result(bean);
    }

    @Override
    public void itemClick(String no, int position, String name) {
        if (mPopWindow.getType() < TeamMultiPopWindow.TYPE_FOU) {
            getCategory(no, false);
        }
    }

    public void showPop(View view) {
        if (mPopWindow == null) {
            getCategory("", true);
        } else {
            if (!mPopWindow.isShowing()) {
                mPopWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            }
        }
    }
}
